﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace guest_management_in_a_hotel
{
    public partial class FrmSaoLuuDL : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        public FrmSaoLuuDL()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFile = new OpenFileDialog();
            oFile.Filter = "bak files (*.bak)|*.bak|All files (*.*)|*.*";
            if (oFile.ShowDialog() == DialogResult.OK)
            {
                tbrestore_path.Text = oFile.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string disk = @"D:\";
            string luu = "CNPM.bak";
            string gach = "_";
            string tenfile="";
            tenfile += DateTime.Now.Hour.ToString() + gach;
            tenfile += DateTime.Now.Minute.ToString() + gach;
            tenfile += DateTime.Now.Second.ToString() + gach;
            tenfile += DateTime.Now.Day.ToString() +gach;
            tenfile += DateTime.Now.Month.ToString() + gach;
            tenfile += DateTime.Now.Year.ToString() + gach;
            tenfile += luu;
            disk += tenfile;
            try
            {
                string sqlBackup = "BACKUP DATABASE [CNPM] TO DISK='"+disk+"'";
                conn = Connect.getConnection();
                conn.Open();
                cmd = new SqlCommand(sqlBackup, conn);
                MessageBox.Show(sqlBackup);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Đã sao lưu cơ sở dữ liệu tại đường dẫn '"+disk+"'");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Backup Database");
                return;
            }
        }

        private void btnquaylai_Click(object sender, EventArgs e)
        {
            FrmMain frmback = new FrmMain();
            frmback.Show();
            this.Hide();
        }

        private void btnkhoiphuc_Click(object sender, EventArgs e)
        {
            try
            {
                string path = tbrestore_path.Text;
                string sqlRestore = "Use master Restore Database [CNPM] from disk='" + path + "'";
                conn.Open();
                cmd = new SqlCommand(sqlRestore, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Database đã được khôi phục ");

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Restore Database");
            }
        }   
    
    }
}
